<?php  get_main_menu("receptionist");
mysql_select_db($database_drycleaners_conn, $drycleaners_conn);
$query_order_payment_by_id = sprintf("SELECT person.first_name,
       person.last_name,
       `order`.created_date,
       `order`.id,
       branch.name,
       `order`.order_no
  FROM    (   (   drycleaners.`order` `order`
               INNER JOIN
                  drycleaners.branch branch
               ON (`order`.branch_id = branch.id))
           INNER JOIN
              drycleaners.customer customer
           ON (`order`.customer_id = customer.id))
       INNER JOIN
          drycleaners.person person
       ON (customer.person_id = person.id)
	   
		  WHERE order.branch_id='".$_SESSION['branch_id']."'
		  AND order.id = %s", GetSQLValueString($_REQUEST['order_id'], "int"));
$order_payment_by_id = mysql_query($query_order_payment_by_id, $drycleaners_conn) or die(mysql_error());
$row_order_payment_by_id = mysql_fetch_assoc($order_payment_by_id);
$totalRows_order_payment_by_id = mysql_num_rows($order_payment_by_id);
 ?>

   <br/>
   <div style="width:90%; margin-left:auto; margin-right:auto;">
    <div class="form_title">

            <strong><?php echo $_REQUEST["action"];?> Order Payment For Order No:</strong><?php echo $row_order_payment_by_id['order_no'] ?>&nbsp;&nbsp;Of &nbsp; <strong>Customer Names:</strong><?php echo $row_order_payment_by_id['first_name']." ".$row_order_payment_by_id['last_name'] ?>
    </div>
    <div class="box"> 
<?php
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "order_payment_form") && (isset($_POST["save"]))) {
	if($_REQUEST['amount']<=$_REQUEST['balance']){
  $insertSQL = sprintf("INSERT INTO order_payment (order_id, branch_id, amount, created_date,created_by, record_status) VALUES (%s,%s, %s, %s, %s, %s)",
                       GetSQLValueString($_REQUEST['order_id'], "int"),
					   GetSQLValueString($_REQUEST['branch_id'], "int"),
                       GetSQLValueString($_REQUEST['amount'], "double"),
					   GetSQLValueString(date("y-m-d"), "date"),
					   GetSQLValueString($loggedin_id, "int"),
                       GetSQLValueString($_REQUEST['record_status'], "text"));

  mysql_select_db($database_drycleaners_conn, $drycleaners_conn);
  $Result1 = mysql_query($insertSQL, $drycleaners_conn) or die(mysql_error());
    if(mysql_affected_rows($drycleaners_conn)>0){
				redirect_drycleaners("order_payment_view&order_id=".$_REQUEST['order_id']."","2");
}
}else{
	echo "The Payment is Greater Than the Remaining Balance on this Order by <strong>".($_REQUEST['amount']-$_REQUEST['balance'])."</strong> Please Make Adjustments<br/>";
	}

}

$colname_order_payment_by_id = "-1";
if (isset($_GET['id'])) {
  $colname_order_payment_by_id = $_GET['id'];
}
mysql_select_db($database_drycleaners_conn, $drycleaners_conn);
$query_order_payment_by_id = sprintf("SELECT order_payment.amount, order_payment.created_date, branch.name
  FROM    drycleaners.order_payment order_payment
       INNER JOIN
          drycleaners.branch branch
       ON (order_payment.branch_id = branch.id) WHERE order_payment.id = %s", GetSQLValueString($colname_order_payment_by_id, "int"));
$order_payment_by_id = mysql_query($query_order_payment_by_id, $drycleaners_conn) or die(mysql_error());
$row_order_payment_by_id = mysql_fetch_assoc($order_payment_by_id);
$totalRows_order_payment_by_id = mysql_num_rows($order_payment_by_id);
?>
<strong>Remaining Balance:</strong> <?php echo $_REQUEST['balance'] ?>
<form action="<?php echo $editFormAction; ?>" method="POST" name="order_payment_form" id="order_payment_form">
  <table align="left">
    <tr>
    	<td><label for="amount">Amount</label></td>
        <td><input name="amount" type="text"  value="<?php echo $row_order_payment_by_id['amount']; ?>"/></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="left">&nbsp;</td>
      <td><?php if(isset($_REQUEST['order_installment_payment_edit'])){	
		echo"<input name='update' type='submit' value='UPDATE' id='submit'/>";
		echo" <a href='.?order_installment_payment_view= true&order_id=".$_REQUEST['order_id']."&back_page=order_payment_view'><input name='add' type='button' value='Cancel' id='submit'/></a>";
		echo"<input type='hidden' name='MM_update' value='order_payment_form' />";	
		}
		else{
			echo"<input name='save' type='submit' value='SAVE' id='submit'/>";
			echo" <a href='.?".$_REQUEST['back_page']."= true&order_id=".$_REQUEST['order_id']."&back_page=order_payment_view'><input name='add' type='button' value='Cancel' id='submit'/></a>";
			echo"<input type='hidden' name='MM_insert' value='order_payment_form' />";	
			}
		?></td>
    </tr>
  </table>
  <input type="hidden" name="order_id" value="<?php echo $_REQUEST["order_id"] ?>" />
  <input type="hidden" name="balance" value="<?php echo $_REQUEST["balance"] ?>" />
    <input type="hidden" name="branch_id" value="<?php echo $_SESSION["branch_id"] ?>" />
  <input type="hidden" name="id" value="<?php echo $row_order_payment_by_id['id']; ?>" />
  <input type="hidden" name="record_status" value="active" />
  <input type="hidden" name="MM_insert" value="order_payment_form" />
</form>
<?php

mysql_free_result($order_payment_by_id);


?>
</div>